package data_structure.offer;

import java.util.Stack;

public class StackPlus {

    Stack<Integer> stack1 = new Stack<>();
    Stack<Integer> stack2 = new Stack<>();//栈顶即为最小值

    public void push(int node) {

        stack1.push(node);
        if (stack2.isEmpty()) {
            stack2.push(node);
        } else {

            if (node < stack2.peek()) {
                stack2.push(node);
            }
        }
    }

    public void pop() {
        //删除的时候要在两个栈中操作
        //the peek of stack2 can only be deleted when two stack's peek is equivalent;
        if (stack1.pop() == stack2.peek()) {
            stack2.pop();
        }
    }

    public int top() {
        return stack1.peek();
    }

    public int min() {
        return stack2.peek();
    }

    public boolean isPopOrder(int[] pushA, int[] popA) {

        if (pushA.length == 0 || popA.length == 0) {
            return false;
        }
        Stack<Integer> stack = new Stack<>();
        int index = 0;
        for (int value : pushA) {
            stack.push(value);
            while (!stack.isEmpty() && stack.peek() == popA[index]) {
                stack.pop();
                index++;
            }
        }
        return stack.isEmpty();
    }

}
